$(function () {
    

    $('#form').bootstrapValidator({

        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },

        fields: {
            username: {
                validators: {
                    //不能为空
                    notEmpty: {
                        message: '用户名不能为空!',
                    },
                    //判断长度
                    stringLength: {
                        min: 2,
                        max: 6,
                        message: '用户名长度必须2到6位之间!'
                    },
                    callback: {
                        message: '用户名不存在'
                    }
                }
            },

            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空!'
                    },
                    stringLength: {
                        min: 6,
                        max: 12,
                        message: '密码长度必须6到12位之间!'
                    },
                    callback: {
                        message: '密码错误'
                    }
                }
            }
        },
        
    })

    //当表单校验成功时，会触发success.form.bv事件
    //发送ajax 请求
    $('#form').on('success.form.bv', function(e){
        //禁止表单的自动提交
        e.preventDefault();
        // console.log(e);
        //获取表单验证实例
        var validator = $('#form').data('bootstrapValidator');
        $.ajax({
            type: 'post',
            url: '/employee/employeeLogin',
                ///employee/employeeLogin
            data: $('#form').serialize(),
            dataType: 'json',
            success: function(res){
                // console.log(res);

                if(res.success) {       
                    location.href = "index.html";
                }

                //根据错误码更新字段样式
                if(res.error == 1000) {
                    validator.updateStatus('username', 'INVALID', 'callback');
                }
                if(res.error == 1001) {
                    validator.updateStatus('password', 'INVALID', 'callback');
                }
            }
        }) 
    })

    //重置表单
    $('.btn-reset').on('click', function(){
        $('#form').data('bootstrapValidator').resetForm();
    })

    

});